package custom

import (
	"dog-blog/app/post/models"
	"dog-blog/app/post/resp"
	"dog-blog/app/post/services"
	"dog-blog/base/apis"

	"github.com/gin-gonic/gin"
)

type Custom struct {
	apis.Api
}

// Retrieval 检索文章
func (e Custom) Retrieval(c *gin.Context) {
	e.Context = c

	services.Retrieval()
	e.Default(resp.OK)
}

// GenerateCaptcha 获取验证码
func (e Custom) GenerateCaptcha(c *gin.Context) {
	e.Context = c

	id, b64s, err := services.CaptchaGenerate()
	if err != nil {
		e.GetLogger().E("GenerateCaptchaHandler error : %s", err.Error())
		e.Default(resp.ERR)
	}

	captcha := models.Captcha{ID: id, B64s: b64s}
	e.Custom(resp.CaptchaResp{BaseResp: resp.BaseResp{ErrCode: resp.OK, ErrDesc: resp.ErrDesc[resp.OK]}, Data: captcha})
}

// GitOption 文章Git操作
func (e Custom) GitOption(c *gin.Context) {
	e.Context = c

	option := c.Param("option")

	if option != "status" && option != "pull" && option != "log" {
		e.Default(resp.ErrNoPermission)
		return
	}

	res, err := services.GitArticle(option)
	if err != nil {
		e.Custom(resp.BaseResp{ErrCode: resp.ERR, ErrDesc: res})
		return
	}
	e.Custom(resp.BaseResp{ErrCode: resp.OK, ErrDesc: res})
}
